/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

/**
 *
 * @author IFedorov
 */
public class Permutation
{

    public Permutation()
    {
    }

    /**
     * Function that returns true if arrays are permutated and false otherwise
     *
     * @param set1
     * @param set2
     * @return are arrays permutated or no
     */
    public boolean arePermutated(Object[] set1, Object[] set2)
    {
        if (theSameObjectsInArrays(set1, set2))
        {
            for (int i = 0; i < set1.length; i++)
            {
                if (!(set1[i].hashCode() == set2[i].hashCode()))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean theSameObjectsInArrays(Object[] obj1, Object[] obj2)
    {
        Object[] set1 = obj1.clone();
        Object[] set2 = obj2.clone();
        if (set1.length != set2.length)
        {
            return false;
        }
        for (int i = 0; i < set1.length; i++)
        {
            Object tmp = set1[i];
            for (int j = 0; j < set2.length; j++)
            {
                if (set2[j] != null && tmp != null && set2[j].getClass().equals(tmp.getClass())
                        && set2[j].hashCode() == tmp.hashCode())
                {
                    set2[j] = null;
                    break;
                }
                else if (j == (set2.length - 1))
                {
                    return false;
                }
            }
        }
        return true;
    }
}
